<?php

class Admin_InstitutesController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    	Zend_Loader::loadClass('InstitutesModel');
    }

    function preDispatch(){
    	/*
    	 * Kiem tra dang nhap
    	*/
    	
    	$auth = Zend_Auth::getInstance();
    	if(strcmp($this->_request->getActionName(),'login')!=0){
    		if (!$auth->hasIdentity()) {
    			$this->_redirect('/admin/index/login');
    		}
    	}
    	/*
    	 * Tra cac thong tin ve moduln cho view
    	*/
    	$this->view->module = $this->_request->getModuleName();
    	$this->view->controller =  $this->_request->getControllerName();
    	$this->view->action =  $this->_request->getActionName();
    	/*
    	 * Load template
    	*/
    	$layoutPath = APPLICATION_PATH  . '/templates/admin/admin';
    	$option = array (	'layout' 		=> 'index',
    			'layoutPath' 	=> $layoutPath );
    	Zend_Layout::startMvc ( $option );
    }
    
    public function indexAction(){
    	
    	$dt = new InstitutesModel();
    	$requestnew = $this->_request;
    	$request = $this->getRequest();
    	if ($this->_request->isPost()){
    		$id = $request->getParam('id');
    		$name = $request->getParam('name');
    		if($name != ""){
    			if((trim($name) != "")){
    				
    					$data = array("name" => $name);
    					if($requestnew->getParam('submit') == 'Thêm'){
    						$dt->insert($data);
    					}
    					else if($requestnew->getParam('submit') == 'Chỉnh sửa'){
    						$dt->update($data,"id=".$id);
    					}
    					$this->view->success = 1;
    			}
    		}
    		else{
    			$this->view->warning = 1;
    		}
    	}
        $data  =  $dt->institutes();

    	$this->view->datas = $data;
    //    Zend_Debug::dump($data);die;
    }
    
    public function editAction(){
    	$layout = $this->_helper->layout();
    	$layout->disableLayout();
    	$request = $this->getRequest();
    	$id = $request->getParam('id');
    	$dt = new InstitutesModel();
    	$this->view->data = $dt->id_institutes($id);
    }
    
    public function deleteAction(){
    	$dt = new InstitutesModel();
    	$request = $this->getRequest();
    	$id = $request->getParam('id');
    	if($id){
    		$dt->delete("id=".$id);
    	}
    	exit();
    }
}